# 公用的自定义工具类
import functools

from flask import g
from flask import current_app, session

from info.models import User
# from sqlalchemy.testing.pickleable import User


def to_index_class(index):
    if index == 0:
        return 'first'
    elif index == 1:
        return 'second'
    elif index == 2:
        return 'third'
    return ''


def query_user_data():
    user_id = session.get('user_id', None)
    user = None
    if user_id:
        # 尝试查询用户的模型
        try:
            from info.models import User
            user = User.query.get(user_id)
        except Exception as e:
            current_app.logger.error(e)
        return user
    return None


def user_login_data(f):
    # 使用functools.wraps 去装饰内层函数，可以保持当前装饰器去装饰的函数 __name__ 的值不变
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        user_id = session.get('user_id', None)
        user = None
        if user_id:
            # 尝试查询用户的模型
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        g.user = user
        return f(*args, **kwargs)
    return wrapper






















